package module1

import (
	"context"
	"fmt"
	"time"
)

func DemoContext() {
	baseCtx := context.Background()
	ctx := context.WithValue(baseCtx, "name", "Phoebe")
	go func(c context.Context) {
		fmt.Println(c.Value("name"))
	}(ctx)

	timeoutCtx, cancel := context.WithTimeout(baseCtx, time.Second)
	fmt.Println(timeoutCtx.Deadline())
	defer cancel()
	fmt.Println(timeoutCtx.Err())
	fmt.Println(<-timeoutCtx.Done())

	go func(c context.Context) {
		ticker := time.NewTicker(1 * time.Second)
		for _ = range ticker.C {
			select {
			case done := <-c.Done():
				fmt.Println("child process interrupt... from child")
				fmt.Println(done)
				return
			default:
				fmt.Printf("default\n")
			}
		}
	}(timeoutCtx)
	time.Sleep(time.Second)

	select {
	case <-timeoutCtx.Done():
		time.Sleep(time.Second)
		fmt.Println("main timeout context process")
	}
}
